var APP = require('../main/app');
var aliPush = api.require('aliPush');
module.exports = {
	HasPush : false,
    PushData : null,
    HasInt : false,
    notifyId: [],
	isRegister: function(callback) {
		aliPush.isRegister(function(ret, err) {
		    if(ret.status) {
		    	$.isFunction(callback) && callback();
		    }
		});
	},
	// 解绑
	unbindAccount: function(account, callback) {
		if(account && account != 'youke') {
			aliPush.unbindAccount({
				account: account
			}, function(ret, err) {
				// alert(JSON.stringify(ret));
				// alert('解绑成功')
				// $.isFunction(callback) && callback();
			});
		}
	},
	bindAccount: function(account, callback) {
		aliPush.bindAccount({
		    account: account
		}, function(ret, err) {
		    // alert(JSON.stringify(ret));
		    $.isFunction(callback) && callback();
		});
	},
	listenEvt: function(callback) {
		var that = this;
		
		
		if(api.systemType=='android') {
			var obj = api.require('notifyChannelModule');
			obj.createNotificationChannel({
			 	id : '1',
				 name : 'jianbingjiancoach',
				 description : 'jianbingjiancoachapp',
				 enableLights : true,
				 enableVibration : true,
			},function(ret, err) {
			 	// alert(JSON.stringify(ret));
			 	if(ret.status==true) {
			 		console.log("createNotificationChannel:success");
			 	} else {
			 		alert(JSON.stringify(err));
			 	}
			});
		}
		
		//接收到消息通知
		aliPush.addEventListener({
		    name: 'onNotification'
		}, function(ret, err) {
			//console.log("push:"+JSON.stringify(ret));
			api.setAppIconBadge({
			    badge : 1
			});
		    $.isFunction(callback) && callback(ret);
		});
		
		//点击通知
		aliPush.addEventListener({
		    name: 'onNotificationOpened'
		}, function(ret, err) {
			console.log("通知栏点击:"+JSON.stringify(ret));
			//清除icon
			// api.setAppIconBadge({
			//     badge : 0
			// });
		    //$.isFunction(callback) && callback(ret);
		    if(ret.status) {
				// 模拟发送通知
				var extraMap = api.systemType == 'ios' ? ret.userInfo : ret.extraMap;
				try{
					if(extraMap) {
						var win_param =  api.systemType == 'ios' ?  extraMap.win_param : JSON.parse(extraMap.win_param);
						console.log('模拟发送通知:'+ win_param.id);
						API.sendEvt('NOTIFICATION',{'waringId':win_param.id});
					}
				}catch(e){
					//TODO handle the exception
				}
		    }
		});
	},
	init: function(StudentId) {
		var that = this;
		//aliPush.clearNotifications(function(ret) {
		    //alert(JSON.stringify(ret));
		//});
		var aliPush = api.require('aliPush');
		api.setAppIconBadge({
		    badge : 0
		});
		aliPush.getDeviceId(function(ret) {
			console.log("DeviceId:");
		    console.log(JSON.stringify(ret));
		});
		that.isRegister(function() {
			if(StudentId && StudentId != 'youke') {
				that.bindAccount(StudentId,function() {
					
				});
				that.listenEvt(function(ret) {
					console.log("onNotification>>>addEventListener>>>>" + JSON.stringify(ret));
				});
			}
		});

		//PUSH
        //其他应用调起
        API.listenEvt('appintent', function(ret, err) {
        	return;
        	//console.log("push1:"+JSON.stringify(ret));
            var Param = !!ret ? ret.appParam : {};
            var params = Param;
            //console.log('appintent:'+JSON.stringify(params));
            that.Push(params);
        });
        //通知栏点击，这里不起作用
        API.listenEvt('noticeclicked', function(ret, err) {
        	//console.log("通知栏点击B:"+JSON.stringify(ret));
        	return;
            if (api.systemType == 'ios') {
                that.Push(ret.value.extra);
            } else {
                that.Push(ret.value.extra);
                //alert(JSON.stringify(ret));
            }
            //console.log('noticeclicked:'+JSON.stringify(params));
        });
	},
	Push : function(params) {
        var that = this;
        that.HasPush = true;
        that.PushData = params;
        if (!that.HasInt) {//如果初始化完成直接打开推送页否则不进行页面跳转   防止闪退
            if(params.hasOwnProperty('alipush')) {
                //AD.toUrl(params.ajpush.extra.url);
            }
            api.setAppIconBadge({
                badge : 0
            });
            that.HasInt = true;
        }
    },

}